﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.filters.*;
	import flash.utils.getDefinitionByName;

	public class Battle extends MovieClip 
	{
		public var MainInfo:BasicInfo;
		
		private var activePlayer:int;
		private var activePlayerToSearch:int;
		private var animationPlayingLength:int;
		private var actionInQueue:String;
		
		public static const EventAttack:String = "Attack";
		public static const EventWait:String = "Wait";
		
		public function Battle(mainInfo:BasicInfo) 
		{
			MainInfo = mainInfo;
			
			init();
			
			determineSettings();
			
			if (MainInfo.PartyLineup[0] != null)
			{
				initializePlayer(statBar1, MainInfo.PartyLineup[0]);
				p1holder.addChild(new BattlePlayer(MainInfo.PartyLineup[0]));
			}
			else
			{
				statBar1.visible = false;
			}
			if (MainInfo.PartyLineup[1] != null)
			{
				initializePlayer(statBar2, MainInfo.PartyLineup[1]);
				p2holder.addChild(new BattlePlayer(MainInfo.PartyLineup[1]));
			}
			else
			{
				statBar2.visible = false;
			}
			if (MainInfo.PartyLineup[2] != null)
			{
				initializePlayer(statBar3, MainInfo.PartyLineup[2]);
				p3holder.addChild(new BattlePlayer(MainInfo.PartyLineup[2]));
			}
			else
			{
				statBar3.visible = false;
			}
			
			addEventListener(Event.REMOVED, Unload);
			addEventListener(Event.ENTER_FRAME, EnterFrame);
			addEventListener(EventAttack, attack);
			addEventListener(EventWait, wait);
		}
		
		private function init():void
		{
			activePlayer = 0;
			activePlayerToSearch = 1;
			animationPlayingLength = 0;
			pointer.Active = false;
			battleMenu.visible = false;
			battleMenu.Active = false;
			actionInQueue = StringHelper.Empty;
		}
		
		private function EnterFrame(event:Event):void
		{
			if (animationPlayingLength <= 0)
			{
				if (MainInfo.PartyLineup[0] != null)
				{
					statBar1.ap.CurrentValue += (Constants.GameSpeed + (MainInfo.PartyLineup[0].Speed / Constants.GameSpeedDivisor));
				}
				if (MainInfo.PartyLineup[1] != null)
				{
					statBar2.ap.CurrentValue += (Constants.GameSpeed + (MainInfo.PartyLineup[1].Speed / Constants.GameSpeedDivisor));
				}
				if (MainInfo.PartyLineup[2] != null)
				{
					statBar3.ap.CurrentValue += (Constants.GameSpeed + (MainInfo.PartyLineup[2].Speed / Constants.GameSpeedDivisor));
				}
				
				switchPlayer();
			}
			else
			{
				animationPlayingLength--;
			}
		}
		
		private function Unload(event:Event):void
		{
			if (event.target.name == this.name)
			{
				removeChildren();
				removeEventListener(Event.ENTER_FRAME, EnterFrame);
			}
		}
		
		private function removeChildren():void
		{
			if (numChildren > 0)
			{
				removeChildAt(0);
				removeChildren();
			}
		}
		
		public function get ActiveStatBar():MovieClip
		{
			switch(activePlayer)
			{
				case 1:
					return statBar1;
				break;
				case 2:
					return statBar2;
				break;
				case 3:
					return statBar3;
				break;
			}
			return null;
		}
		
		public function get ActiveFighterPlaceholder():MovieClip
		{
			switch(activePlayer)
			{
				case 1:
					return p1holder;
				break;
				case 2:
					return p2holder;
				break;
				case 3:
					return p3holder;
				break;
			}
			return null;
		}
		
		private static function initializePlayer(statBar:MovieClip, baseStats:BaseStats):void
		{
			statBar.hp.BarType = Constants.BarType_HEALTH;
			statBar.hp.MaxValue = baseStats.MaxHealth;
			statBar.hp.CurrentValue = baseStats.Health;
			statBar.hp.InitializeValues();
			
			statBar.mp.BarType = Constants.BarType_MAGIC;
			statBar.mp.MaxValue = baseStats.MaxMagic;
			statBar.mp.CurrentValue = baseStats.Magic;
			statBar.mp.InitializeValues();
			
			statBar.ap.BarType = Constants.BarType_ACTION;
			statBar.ap.InitializeValues();
			
			statBar.playerName.text = baseStats.Name;
		}
		
		private function switchPlayer():void
		{
			if (activePlayer == 0)
			{
				if (activePlayerToSearch > 3)
				{
					activePlayerToSearch = 1;
				}
				
				if (activePlayerToSearch == 1)
				{
					if (statBar1.ap.DisplayValue == Constants.MaxActionPoints)
					{
						activePlayer = 1;
						applyGlow(statBar1);
						popupMenu();
						return;
					}
					activePlayerToSearch++;
				}
				if (activePlayerToSearch == 2)
				{
					if (statBar2.ap.DisplayValue == Constants.MaxActionPoints)
					{
						activePlayer = 2;
						applyGlow(statBar2);
						popupMenu();
						return;
					}
					activePlayerToSearch++;
				}
				if (activePlayerToSearch == 3)
				{
					if (statBar3.ap.DisplayValue == Constants.MaxActionPoints  && activePlayerToSearch == 3)
					{
						activePlayer = 3;
						applyGlow(statBar3);
						popupMenu();
						return;
					}
					activePlayerToSearch++;
				}
			}
		}
		
		private function attack(event:Event):void
		{
			actionInQueue = EventAttack;
			battleMenu.Active = false;
			pointer.InitializeValues();
		}
		
		private function wait(event:Event):void
		{
			actionInQueue = EventWait;
			PerformedAction(false, null);
			activePlayerToSearch++;
		}
		
		public function PerformedAction(resetAP:Boolean, target:MovieClip):void
		{
			if (resetAP)
			{
				animationPlayingLength = Constants.AnimationLength;
				ActiveStatBar.ap.CurrentValue = 0;
			}
			
			var attacker:MovieClip = ActiveFighterPlaceholder.getChildAt(0) as MovieClip;
			
			battleMenu.visible = false;
			battleMenu.Active = false;
			ActiveStatBar.filters = [];
			activePlayer = 0;
			
			switch (actionInQueue)
			{
				case EventAttack:
					executeAttack(attacker, target)
				break;
				case EventWait:
				break;
			}
		}
		
		private function executeAttack(attacker:MovieClip, target:MovieClip)
		{
			attacker.ExecuteAttack(target);
		}
		
		private function determineSettings()
		{
			var copyBkg:Class = getDefinitionByName(MainInfo.BackgroundName) as Class;
			var overwoldBackground:Background = (new copyBkg);
			var chance = Constants.Random(1, 100);
			
			switch(overwoldBackground.AreaName)
			{
				case Constants.Area_Beach:
					backdrop.gotoAndStop(1);
					if (chance <= 10)
					{
						e1holder.addChild(new Enemy_Crab());
						e2holder.addChild(new Enemy_Crab());
						e3holder.addChild(new Enemy_Crab());
					}
					else if (chance <= 40)
					{
						e1holder.addChild(new Enemy_Crab());
					}
					else
					{
						e1holder.addChild(new Enemy_Crab());
						e2holder.addChild(new Enemy_Crab());
					}
				break;
				default:
					backdrop.gotoAndStop(1);
					if (chance <= 10)
					{
						e1holder.addChild(new Enemy_Crab());
						e2holder.addChild(new Enemy_Crab());
						e3holder.addChild(new Enemy_Crab());
					}
					else if (chance <= 40)
					{
						e1holder.addChild(new Enemy_Crab());
					}
					else
					{
						e1holder.addChild(new Enemy_Crab());
						e2holder.addChild(new Enemy_Crab());
					}
				break;
			}
		}
		
		private function popupMenu():void
		{
			battleMenu.visible = true;
			battleMenu.Active = true;
		}
		
		private function applyGlow(clip:MovieClip):void
		{
			var myGlow:GlowFilter = new GlowFilter();
			myGlow.color = 0xFFFF00;
			clip.filters = [myGlow];
		}
	}
}